//
//  LSYGuideLoginView.swift
//  LSYWeibo
//
//  Created by 刘诗岩 on 16/4/7.
//  Copyright © 2016年 刘诗岩. All rights reserved.
//

import UIKit

/** 声明协议 可选方法需要加 @objc */
@objc protocol LSYGuideLoginViewDelegate: NSObjectProtocol{
    
    optional func userWillLogin()
    
    optional func userWillRegister()
    
}

class LSYGuideLoginView: UIView {
    
    /** 设置代理属性 */
    weak var delegate: AnyObject?

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        let value: CGFloat = 237 / 255
        
        self.backgroundColor = UIColor(red: value, green: value, blue: value, alpha: 1.0)
        
        // 添加子控件
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /** 给外界提供方法设置界面 */
    // 设置默认值,表示可以不传参数
    func setGuideViewInfoWith(tipText: String, imageName: String, isHome: Bool = false){
        
        // 判断是否是首页
        if isHome == true{
            
            // 开始旋转
            startRotation()
        }else{
            
            // 隐藏大房子
            houseView.hidden = true
            
            // 将圈圈视图移动至最前
            bringSubviewToFront(circleView)
        }
        
        tipLabel.text = tipText
        circleView.image = UIImage(named: imageName)
    }
    
    /** 给圈圈图添加动画 */
    private func startRotation(){
        
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.repeatCount = MAXFLOAT
        animation.duration = 15
        animation.toValue = 2 * M_PI
        animation.removedOnCompletion = false
        
        circleView.layer.addAnimation(animation, forKey: nil)
    }
    
    /** 给控件设置约束 */
    override func layoutSubviews() {
        super.layoutSubviews()
        
        // 旋转图片约束
        circleView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
        }
        
        // 房子图片约束
        houseView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self.circleView.snp_center)
        }
        
        // 遮罩图片约束
        coverView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(self.circleView.snp_center)
        }
        
        // 根据图片像素,一律以圆圈的大小进行布局
        tipLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(self.circleView.snp_bottom)
            make.centerX.equalTo(self.circleView.snp_centerX)
            make.width.equalTo(230)
            
            // 添加高度约束,解决文本行数不同,按钮位置不同的 bug
            make.height.equalTo(40)
        }
        
        loginButton.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(self.tipLabel.snp_bottom).offset(30)
            make.centerX.equalTo(self.tipLabel.snp_centerX).offset(-40)
            make.width.equalTo(60)
        }
        
        registButton.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(self.tipLabel.snp_bottom).offset(30)
            make.centerX.equalTo(self.tipLabel.snp_centerX).offset(40)
            make.width.equalTo(60)
        }
    }
    
    
    /** 加载控件 */
    private func setupUI(){
        
        addSubview(circleView)
        addSubview(coverView)
        addSubview(houseView)
        addSubview(loginButton)
        addSubview(registButton)
        addSubview(tipLabel)
        
    }
    
    
    // MARK: - 懒加载控件
    /** 屏幕中间图 */
    private lazy var circleView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    private lazy var houseView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    private lazy var coverView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    /** 登录按钮 */
    private lazy var loginButton: UIButton = {
        
        let button = UIButton(type: .Custom)
        
        button.setTitle("登录", forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        // 拉伸图片并赋值
        let image = UIImage(named: "common_button_white_disable")!
        let capW = Int(image.size.width * 0.5)
        let capH = Int(image.size.height * 0.5)
        let img = image.stretchableImageWithLeftCapWidth(capW, topCapHeight: capH)
        button.setBackgroundImage(img, forState: .Normal)
        
        button.addTarget(self, action: "loginButtonDidClick", forControlEvents: .TouchUpInside)
        
        return button
    }()
    
    @objc private func loginButtonDidClick(){
        
        // 第一个问号,判断是否有代理对象,第二个问号,判断是否实现了代理方法
        self.delegate?.userWillLogin?()
    }
    
    
    /** 注册按钮 */
    private lazy var registButton: UIButton = {
        
        let button = UIButton(type: .Custom)
        
        button.setTitle("注册", forState: .Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        // 拉伸图片并赋值
        let image = UIImage(named: "common_button_white_disable")!
        let capW = Int(image.size.width * 0.5)
        let capH = Int(image.size.height * 0.5)
        let img = image.stretchableImageWithLeftCapWidth(capW, topCapHeight: capH)
//        image.resizableImageWithCapInsets(UIEdgeInsetsMake(capH, capW, capH, capW), resizingMode: .Stretch)
        button.setBackgroundImage(img, forState: .Normal)
        
        button.addTarget(self, action: "registButtonDidClick", forControlEvents: .TouchUpInside)
        
        return button
    }()
    
    @objc private func registButtonDidClick(){
        
        // 第一个问号,判断是否有代理对象,第二个问号,判断是否实现了代理方法
        self.delegate?.userWillRegister?()
    }
    
    /** 提示文案 */
    private lazy var tipLabel : UILabel = {
        
        let tip = UILabel()
        
        tip.text = "加入我们,让生活更精彩"
        tip.textColor = UIColor.darkGrayColor()
        tip.font = UIFont.systemFontOfSize(14)
        tip.sizeToFit()
        tip.preferredMaxLayoutWidth = 230
        tip.textAlignment = .Center
        tip.numberOfLines = 0
        
        return tip
    }()

    
    
    
    
    
    
    
    
    
    
    
    
    
}
